<?php

class ftpEoddata {
  public $server="ftp.eoddata.com";
  public $error="";
  public $downloadedFiles=array();
  public $exchanges=array(
    "INDEX",
    "COMEX",
    "NASDAQ",
    "NYSE",
    "FOREX",
    "CBOT"
  );
  public $exchangesSplits=array(
    "NASDAQ",
    "NYSE"      
  );
  public  $retry=5;
  private $con;
  private $user;
  private $pass;
  private $log;
  private $db;
  
  public function __construct($user, $pass, $log=false) {
    $this->user = $user;
    $this->pass = $pass;
    if($log){
      $this->log= true;
      $this->db = $log;
    } else 
      $this->log=false;
  }
  
  public function loginToEoddata() {
    $this->con = ftp_connect($this->server);
    if (!$this->con) {
      $this->error="Cannot connect to ".$this->server.".\n";
      if ($this->log) $this->db->insert_connection_logentry("connect", true, "Connection to EODDATA failed");
      return false;
    }
    if (!ftp_login($this->con, $this->user, $this->pass)) {
      $this->error="User ".$this->user." cannot login to ".$this->server.".\n";
      if ($this->log) $this->db->insert_connection_logentry("connect", true, "Login to EODDATA failed");
      return false;
    } else {
      if ($this->log) $this->db->insert_connection_logentry("connect", false, "Connection to EODDATA established and login successful");     
    }
    return true;
  }
  
  public function listDirContents($rdir=".") {
    $contents = ftp_nlist($this->con, $rdir);
    return($contents);
  }
  
  public function listRawDirContents($rdir=".") {
    return(ftp_rawlist($this->con, $rdir));   
  }
  
  public function downloadAllTxtFiles($ldir=".", $rdir=".") {
    if (!chdir($ldir)) {
      $this->error="Cannot access local directory ".$ldir.".\n";
      return false;
    }
    if ($rdir=="./splits") {
      $txtfiles = $this->filterSplitFiles($this->listDirContents($rdir));      
    } else {
      $txtfiles = $this->filterTxtFiles($this->listDirContents($rdir));      
    }
    foreach($txtfiles as $file) {
      $success=false;
      $retrycount = 0;
      while($success==false & $retrycount < $this->retry) {
        if (!ftp_get($this->con, $ldir."/".$file, $rdir."/".$file, FTP_ASCII)) {
          $retrycount++;
        } else {
          $success=true;
        }
        sleep(2);
      }
      if (!$success) {
        $this->error.="Cannot retrieve file ".$file.".\n";
        if ($this->log) $this->db->insert_retriever_logentry("download", true, $file, 0, "Error while downloading file ($retrycount)");
        $success=false;
      } else {
        $this->downloadedFiles[]=$file;
        $size = filesize($ldir."/".$file);
        if (is_numeric($size)) {
          if ($this->log) $this->db->insert_retriever_logentry("download", false, $file, $size , "Successfully downloaded file ($retrycount)");             
        } else {
          $this->error.="Problem with filesize for file ".$file.".\n";
          if ($this->log) $this->db->insert_retriever_logentry("download", true, $file, 0 , "Problem with file download ($retrycount)");             
        }
      }
      sleep(1);
    }
    return $success;
  }
  
  public function disconnectFromEoddata() {
    $ret= ftp_close($this->con);
    if ($this->log) $this->db->insert_connection_logentry("disconnect", !$ret, "Disconnect from EODDATA");
    return $ret;
  }

  private function filterTxtFiles($arr) {
    $retarr=array();
    foreach ($arr as $a) {
      if (strpos($a, ".txt") > 0 & strpos($a, "_") > 0) {
        $exp=explode("_", $a);
        if (in_array($exp[0], $this->exchanges)) {
          // only download wanted files
          $retarr[]=$a;
        }
      }
    }
    return $retarr;
  }
  
  private function filterSplitFiles($arr) {
    $retarr=array();
    foreach ($arr as $a) {
      if (strpos($a, ".txt") > 0 ) {
        $exp=explode(".", $a);    
        if (in_array($exp[0], $this->exchangesSplits)) {
          // only download wanted files
          $retarr[]=$a;
        }
      }    
    }   
    return $retarr;
  }
}

?>
